import commonAPI from '../../api/api'
const FN = require('../../utils/public');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    repeatRecordsArr: [],
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu(); // 禁止分享
  },

  initData:function(freshType){
    var that = this;
    commonAPI.getRepeatRecords({ "pageNum": that.data.page,
    pageSize: 15 }).then(res => {
      if (res.code == '200') {
        var temArr = that.data.repeatRecordsArr;
      setTimeout(() => {
        wx.hideNavigationBarLoading(); // 完成停止加载图标
        wx.stopPullDownRefresh(); // 停止下拉

        if (res.data.list.length == 0) {
          FN.Toast('没有更多数据了')
        }else{
          var newWordInfoArr = that.delRepeat(res.data.list);
          // 数据分装以时间为纬度
          newWordInfoArr = that.dealData(newWordInfoArr,res.data.list);
          if (freshType == "2") {
            temArr.push.apply(temArr, newWordInfoArr); //上拉加载
            that.setData({
              repeatRecordsArr: temArr
            })
          }else if(freshType == "1"){
            that.setData({
              repeatRecordsArr: newWordInfoArr
            })
          }
        }
      },500);
      }
    })
  },

  // 去重复 
  delRepeat:function(temArr){
    var dateArr = new Array();
    // 获取日期到数组
    for (var i in temArr) {
      dateArr.push(temArr[i].voiceResultDate);
    }

    // 去重复
    var newdateArr = Array.from(new Set(dateArr));
    var newWordInfoArr = new Array();
    for (var i in newdateArr) {
      var obj = {};
      obj.time = newdateArr[i];
      obj.wordInfoArr = [];
      newWordInfoArr.push(obj);
    }
    return newWordInfoArr;
  },

  // 处理数据 数据分装以时间为纬度
  dealData: function(newWordInfoArr, temArr) {
    const wordInfoMap = new Map(newWordInfoArr.map(info => [info.time, { ...info, wordInfoArr: [] }]));
    temArr.forEach(item => {
        if (wordInfoMap.has(item.voiceResultDate)) {
            const { voiceResultDate, voiceResultInfo } = item;
            wordInfoMap.get(voiceResultDate).wordInfoArr.push({ ...item, voiceResultInfo: JSON.parse(voiceResultInfo) });
        }
    });
    const result = [...wordInfoMap.values()];
    return result;
},
  
  // 再次跟读
  repeatAgain: function(e) {
    var historyRepeatObj = e.currentTarget.dataset.voiceresultinfo;
    wx.navigateTo({
      url: '../homeworkrepeat/homeworkrepeat',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
          console.log(data)
        },
        someEvent: function(data) {
          console.log(data)
        }
      },
      success: function(res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { scene: historyRepeatObj,getIntoType:'other' })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.initData("1");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      page:1
    })
    that.initData("1");
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var i = this.data.page;
    this.setData({
      page:++i
    })
    this.initData("2");
  },

})